<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <ul>
        <li><router-link to="/">Home</router-link></li>
        <li><router-link to="/parent/child/grandson">Parent</router-link></li>
      </ul>
      <router-view></router-view>
    </div>
    <script src="vue.js"></script>
    <script src="vue-router.js"></script>
    <script>
      const Home = {
        template: `<div>Home</div>`,
      };
      const Parent = {
        template: `
        <div>
          <p>Parent</p>
          <router-view></router-view>
        </div>`,
         errorCaptured(error){
          console.log('Parent errorCaptured')
        }
      };
      const Child = {
        template: `
        <div>
          <p>Child</p>
          <p>{{msg}}</p>
          <router-view></router-view>
        </div>`,
        data(){
          return {msg:'hello'};
        },
        errorCaptured(error){
          console.log('Child errorCaptured')
          this.msg = '发生错误了'
          return false;
        }
      };
      const Grandson = {
        template: `
        <div>
          <p>Grandson</p>
        </div>`,
        created(){
          throw new Error('孙子错了');
        }
      };
      const routes = [
        { 
          name: "home", 
          path: "/", 
          component: Home 
        },
        { 
          name: "parent", 
          path: "/parent", 
          component: Parent,
          children:[
            {
              path:'child',
              component:Child,
              children:[
                {
                  path:'grandson',
                  component:Grandson
                }
              ]
            }
          ] 
        }
      ];
      var router = new VueRouter({
        routes,
      });
      Vue.config.errorHandler = function(err,vm,info){
        console.log(err);//抛出的错误对象
        console.log(vm);//Home组件实例
        console.log(info);//created hook
      }
      var vm = new Vue({
        el: "#app",
        router,
      });
    </script>
  </body>
</html>
